/* packet-eth.c
 * Routines for ethernet packet disassembly
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 1998 Gerald Combs
 *
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <gtk/gtk.h>

#include <stdio.h>

#include <pcap.h>

#include "packet.h"
#include "ethereal.h"
#include "etypes.h"

void
dissect_eth(const u_char *pd, frame_data *fd, GtkTree *tree) {
  guint16    etype, length;
  gchar      etype_str[][10] = {"IP", "ARP", "RARP", "AppleTalk", "AARP"};
  int        offset = 14;
  GtkWidget *fh_tree, *ti;

  if (fd->win_info[0]) {
    strcpy(fd->win_info[2], ether_to_str((guint8 *)&pd[0]));
    strcpy(fd->win_info[1], ether_to_str((guint8 *)&pd[6]));
    strcpy(fd->win_info[4], "Ethernet II");
  }

  etype = (pd[12] << 8) | pd[13];
  if (etype <= IEEE_802_3_MAX_LEN) {
    length = etype;
    /* To do: check for 802.2 LLC and SNAP info */
    etype  = (pd[20] << 8) | pd[21];
    if (fd->win_info[0]) { sprintf(fd->win_info[4], "802.3 (0x%04x)", etype); }
    if (tree) {
      ti = add_item_to_tree(GTK_WIDGET(tree), 0, 22,
        "IEEE 802.3 (%d on wire, %d captured)", fd->pkt_len, fd->cap_len);
      fh_tree = gtk_tree_new();
      gtk_tree_item_set_subtree(GTK_TREE_ITEM(ti), fh_tree);
      add_item_to_tree(fh_tree, 0, 6, "Destination: %s",
        ether_to_str((guint8 *) &pd[0]));
      add_item_to_tree(fh_tree, 6, 6, "Source: %s",
        ether_to_str((guint8 *) &pd[6]));
      add_item_to_tree(fh_tree, 12, 2, "Length: %d", length);
      add_item_to_tree(fh_tree, 14, 3, "802.2 LLC: 0x%02x/0x%02x/0x%02x",
        pd[14], pd[15], pd[16]);
    }
    offset = 22;
  } else if (tree) {
    ti = add_item_to_tree(GTK_WIDGET(tree), 0, 14,
      "Ethernet II (%d on wire, %d captured)", fd->pkt_len, fd->cap_len);
    fh_tree = gtk_tree_new();
    gtk_tree_item_set_subtree(GTK_TREE_ITEM(ti), fh_tree);
    add_item_to_tree(fh_tree, 0, 6, "Destination: %s",
      ether_to_str((guint8 *) &pd[0]));
    add_item_to_tree(fh_tree, 6, 6, "Source: %s",
      ether_to_str((guint8 *) &pd[6]));
  }
  
  switch (etype) {
    case ETHERTYPE_IP:
      if (tree) {
        add_item_to_tree(fh_tree, offset - 2, 2, "Type: IP (0x%04x)",
          etype);
      }
      dissect_ip(pd, offset, fd, tree);
      break;
    case ETHERTYPE_ARP:
      if (tree) {
        add_item_to_tree(fh_tree, offset - 2, 2,
          "Type: ARP (0x%04x)", etype);
      }
      dissect_arp(pd, offset, fd, tree);
      break;
    case ETHERTYPE_REVARP:
      if (tree) {
        add_item_to_tree(fh_tree, offset - 2, 2,
          "Type: RARP (0x%04x)", etype);
      }
      dissect_arp(pd, offset, fd, tree);
      break;
    case ETHERTYPE_ATALK:
      if (tree) {
        add_item_to_tree(fh_tree, offset - 2, 2,
          "Type: AppleTalk (0x%04x)", etype);
      }
      if (fd->win_info[0]) { strcpy(fd->win_info[3], etype_str[3]); }
      break;
    case ETHERTYPE_AARP:
      if (tree) {
        add_item_to_tree(fh_tree, offset - 2, 2,
          "Type: AARP (0x%04x)", etype);
      }
      if (fd->win_info[0]) { strcpy(fd->win_info[3], etype_str[4]); }
      break;
    default:
      if (tree)
        add_item_to_tree(fh_tree, offset - 2, 2,
          "Type: Unknown (0x%04x)", etype);
      if (fd->win_info[0]) { sprintf(fd->win_info[3], "0x%04x", etype); }
      break;
  }
}
